package com.yubest.demo.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.*;

/**
 * @program: spring-boot-encrypt
 * @ClassName SginUtil
 * @description:
 * @author: 黄涛
 * @create: 2023-12-01 15:25
 * @Version 1.0
 **/
public class SiginUtil {

    /**
     * 生成签名
     * @param parameters
     * @param key 商户ID
     * @return
     */
    public static String createSign(SortedMap<String,Object> parameters, String key){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + key);
        String sign = SecureUtil.md5(sb.toString()).toUpperCase();
        return sign;
    }

//    /**
//     * 签名参数
//     * @param responseModel 响应数据签名返回给调用者
//     * @return
//     */
//    public  static SortedMap<String,String> createParameters(ResponseModel responseModel){
//        SortedMap<String,String> sortedMap = new TreeMap<String,String>();
//        if(responseModel!=null) {
//            sortedMap.put("timestamp", Convert.toStr(responseModel.getTimestamp()) );
//            sortedMap.put("version", Convert.toStr(responseModel.getVersion()));
//            JSONObject json = JSONUtil.parseObj(responseModel, false);
//            if(responseModel.getData()!=null) {
//                sortedMap.put("data", json.get("data").toString());
//            }
//        }
//        return sortedMap;
//
//    }

}
